package com.example.android.smalltest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private int score = 0;
    private int getScore = 20;
    private static Toast mToast = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    //定义个Toast的显示方法，如果存在就不继续创建，避免性能消耗
    private void show(String text) {
        if (mToast == null) {
            mToast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(text);
        }
        mToast.show();
    }

    /**
     * 提交按钮的点击方法
     *
     * @param 传入的view
     */
    public void CommitAnswer(View view) {

        //获取输入框的text
        EditText textField = (EditText) findViewById(R.id.name_edit_textView);
        String name = textField.getText().toString();

        //拼接显示的文本
//        String message = "Congratulations  " + name + "\n" + (calculateScore() + score);
        String sAgeFormat = getResources().getString(R.string.message_text);
        String message = String.format(sAgeFormat, name, (calculateScore() + score));
        //提示框显示的文本
        String str = String.format(getResources().getString(R.string.show_text), name);

        show(str);

        //调用textView的显示方法
        displayMessage(message);

    }

    /**
     * 计算复选框所得的分数
     *
     * @return 所得分数
     */
    private int calculateScore() {

        int number = 0;
        //通过ID找到对应的checkbox
        CheckBox activityCheckBox = (CheckBox) findViewById(R.id.activity_checkbox);
        CheckBox ServiceCheckBox = (CheckBox) findViewById(R.id.Service_checkbox);
        CheckBox ContentProviderCheckBox = (CheckBox) findViewById(R.id.Content_Provider_checkbox);
        CheckBox BroadcastReceiverCheckBox = (CheckBox) findViewById(R.id.BroadcastReceiver_checkbox);

        //通过ID找到对应的RadioButton
        RadioButton fifteenRadio = (RadioButton) findViewById(R.id.fifteen_Radio_View);
        boolean hasFifteenRadio = fifteenRadio.isChecked();

        if (hasFifteenRadio) {
            number = number + getScore;
        }

        //获取复选框的选中状态的bool值
        boolean hasActivity = activityCheckBox.isChecked();
        boolean hasService = ServiceCheckBox.isChecked();
        boolean hasContentProvider = ContentProviderCheckBox.isChecked();
        boolean hasBrodcast = BroadcastReceiverCheckBox.isChecked();

        //使用if语句判断是否被选中，选中得20分，未选中不得分
        if (hasActivity) {
            number = number + getScore;
        }

        if (hasService) {
            number = number + getScore;
        }

        if (hasBrodcast) {
            number = number + getScore;
        }

        if (hasContentProvider) {
            number = number + getScore;
        }

        return number;
    }

    /**
     * 分数显示的TextView
     *
     * @param传入要显示的text
     */
    private void displayMessage(String message) {
        TextView orderSummaryTextView = (TextView) findViewById(R.id.score_text_view);
        orderSummaryTextView.setText(message);
    }


}
